package cn.com.yang.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 线程池Future
 * TODO
 * @author yangyijin
 * @since 2024年4月19日-下午4:38:34
 */
public class TestExecutorServiceFuture {
	public static void main(String[] args) {
		ExecutorService exeService = Executors.newFixedThreadPool(4);
		List<Future<String>> futures = new ArrayList<>();
		for(int i=0;i<10;i++) {
			Future future = exeService.submit(()->{
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				return "result";
			});
			futures.add(future);
		}
		//等待结果
		List<String> results = new ArrayList<>();  
		for (Future<String> future : futures) {  
		    try {  
		        results.add(future.get());  
		    } catch (InterruptedException | ExecutionException e) {  
		        e.printStackTrace();  
		    }  
		}
	}
}
